

#include "client.h"




volatile int STOP=FALSE;

void signal_handler_IO (int status);    //definition of signal handler
int wait_flag=TRUE;                     //TRUE while no signal received
//FILE *input;
//FILE *output;
int status;
int 	sockfd;
int 	len;
struct sockaddr_in address;
int 	result;
size_t	nbytes;
int	a;
char 	*check;

main()
{
	//char Param_strings[7][80];
	//char message[90];

	int fd, c, res, i, n=0, loop, m=0;
	char In1, Key;
	struct termios oldtio, newtio;       //place for old and new port settings for serial port
	//struct termios oldkey, newkey;       //place tor old and new port settings for keyboard teletype
	struct sigaction saio;               //definition of signal action
	char buf[255];                       //buffer for where data is put
	char buf2[255];
	check = buf;

	//input = fopen("/dev/tty", "r");      //open the terminal keyboard
	//output = fopen("/dev/tty", "w");     //open the terminal screen

	/*tty = open("/dev/tty", O_RDWR | O_NOCTTY | O_NONBLOCK); //set the user console port up
	tcgetattr(tty,&oldkey); // save current port settings   //so commands are interpreted right for this program
	// set new port settings for non-canonical input processing  //must be NOCTTY
	newkey.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
	newkey.c_iflag = IGNPAR;
	newkey.c_oflag = 0;
	newkey.c_lflag = 0;       //ICANON;
	newkey.c_cc[VMIN] = 1;
	newkey.c_cc[VTIME] = 0;
	tcflush(tty, TCIFLUSH);
	tcsetattr(tty,TCSANOW,&newkey);*/
            
while(1)
{
	/* Create a socket for the client. */
		sockfd = socket(AF_INET, SOCK_STREAM, 0);
		
		/* Socket name, as agreed with the server. */
		address.sin_family = AF_INET;
		//address.sin_addr.s_addr = inet_addr(argv[1]);
		address.sin_addr.s_addr = inet_addr("192.168.1.222");
		address.sin_port = 2129;
		len = sizeof(address);
		
		/* Connect our socket to the server's socket. */
		result = connect(sockfd, (struct sockaddr *)&address, len);
		if(result == -1)
		{
			perror("OOPS!!: connect fnc. error");			// show message when program error
			exit(1);
		}



	//open the device(com port) to be non-blocking (read will return immediately)
	fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);

	//install the serial handler before making the device asynchronous
	saio.sa_handler = signal_handler_IO;
	sigemptyset(&saio.sa_mask);   //saio.sa_mask = 0;
	saio.sa_flags = 0;
	saio.sa_restorer = NULL;
	sigaction(SIGIO,&saio,NULL);

	// allow the process to receive SIGIO
	fcntl(fd, F_SETOWN, getpid());
	// Make the file descriptor asynchronous (the manual page says only
	// O_APPEND and O_NONBLOCK, will work with F_SETFL...)
	fcntl(fd, F_SETFL, FASYNC);

	tcgetattr(fd, &oldtio); // save current port settings 
	// set new port settings for canonical input processing 
	newtio.c_cflag = B9600 | CRTSCTS | CS8 | 0 | 0 | 0 | CLOCAL | CREAD;
	newtio.c_iflag = IGNPAR;
	newtio.c_oflag = 0;
	newtio.c_lflag = 0;       //ICANON;
	newtio.c_cc[VMIN]=1;
	newtio.c_cc[VTIME]=0;
	tcflush(fd, TCIFLUSH);
	tcsetattr(fd,TCSANOW,&newtio);

	// loop while waiting for input. normally we would do something useful here
	while (STOP==FALSE)
	{
		
        
		if (wait_flag==FALSE)  //if input is available
		{
          		res = read(fd,buf,255);
			buf[res] = '\0';
           		 if (res==1)
           		 {
				buf2[n] = buf[0];
				write(fd,&buf,1);
				n++;
				m=0;
				
				printf("%s\n", buf);
           		 }
			else if(res>1)
			{
				printf("%s\n", buf);
				m=1;
			} 
			

			if(*check=='\r')
			{
				wait_flag = TRUE;      /* wait for new input */
				STOP = TRUE;
				
			}
			//printf("%s\n",check);
			//check++;
			
			
			
		}
		
		

	}
	//wait_flag = FALSE;
	
	buf2[n]='\0';
	n = 0;
	tcsetattr(fd,TCSANOW,&oldtio);
	close(fd);        //close the com port

	

	if(m=1)
	{	
		/* Write data to server via sockfd. */
			nbytes = sizeof(buf2);
			result = write(sockfd, buf2, nbytes);
			/* Debug */
			printf("string to server: %s\n", buf2);
			if(result == -1)
			{
				perror("OOPS!!: write fnc. error");
			}
	}
	else if(m=0)
	{
		/* Write data to server via sockfd. */
			nbytes = sizeof(buf);
			result = write(sockfd, buf, nbytes);
			/* Debug */
			printf("string to server: %s\n", buf);
			if(result == -1)
			{
				perror("OOPS!!: write fnc. error");
			}
	}
		/* Close socket and exit program. */
		close(sockfd);
		//exit(0);
	STOP = FALSE;
}	
}

void signal_handler_IO (int status)
{
	//printf("received SIGIO signal.\r\n");
	wait_flag = FALSE;
	
}
